from flask import Blueprint, request
from static.connect.conn_mysql import Connect
from static.api.access_token import Card

app_get_history = Blueprint('app_get_history', __name__)
@app_get_history.route('/app_get_history', methods=['POST'])
def get_history():
    user_id = request.form.get('user_id')
    access_token = request.form.get('access_token')
    print([user_id, access_token])
    if not all([user_id, access_token]):
        return {
            'status': -1,
            'errorCode': 61,
            'errorMsg': '请检查你提交的参数',
            'resultBody': None,
        }
    # 令牌验证
    with Card()as C:
        access_token_notify = C.access_tokens(access_token, user_id)
        if access_token_notify["status"] == -1:
            return access_token_notify
    with Connect() as c:
        sql = "SELECT * FROM call_car WHERE user_id=%s"
        result = c.select_all(sql, (user_id,))
    for i in result:
        i['go_time'] = i['go_time'].__str__()
        i['regedate'] = i['regedate'].__str__()
    return {
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": result,
    }
